package test.sgone.cn.mulimagedemo;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import me.nereo.multi_image_selector.MultiImageSelectorActivity;

public class MainActivity extends AppCompatActivity {
    public static int REQUEST_IMAGE = 100;
    private GridView myGrideView;
    private Button button;
    private GridImageAdapter adapter;
    private ArrayList<String> selectData = new ArrayList<String>();
    private static int MAX_PIC_NUM = 3;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
        initView();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    private void initView() {
        //addDefaltImg();
        myGrideView = (GridView) findViewById(R.id.img_gv);
        adapter = new GridImageAdapter(this, MAX_PIC_NUM);
        // adapter.setData(selectData);
        myGrideView.setAdapter(adapter);
        myGrideView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("onItemClick path==", adapter.getData().get(position));


                if (adapter.getData().get(position).contains(GridImageAdapter.DEFAULT_IMG)) {
                    // removeDefaltImg(selectData);
                    goImg();
                } else {
                    Intent intent = new Intent(MainActivity.this,
                            BrowerImageActivity.class);
                    Bundle bundle1 = new Bundle();
                    bundle1.putStringArrayList("dataList", selectData);
                    bundle1.putInt("position", position);
                    intent.putExtras(bundle1);
                    startActivity(intent);
                }

            }
        });
        adapter.notifyDataSetChanged();

        button = (Button) findViewById(R.id.btn_click);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //  removeDefaltImg(selectData);
                goImg();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE) {
            if (resultCode == RESULT_OK) {
                // 获取返回的图片列表
                ArrayList<String> path = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                Log.e("path===", path.get(0) + "adataper count" + path.size());
                selectData.addAll(path);
                for (int i = 0; i < path.size(); i++) {
                    //selectData.clear();
                    //  selectData.addAll(path);
                    //addDefaltImg();
                    adapter.addPic(path.get(i));
                }
                //  adapter.setData(selectData);

                // 处理你自己的逻辑 ....
            }
        }
    }
/*   private void addDefaltImg()
   {
       if(selectData.size() < 3)
           selectData.add("camera_default");
   }*/

/*    private void removeDefaltImg(ArrayList arraylist)
    {
        for(int i=0;i<arraylist.size();i++)
        {
            if(((String)arraylist.get(i)).contains("camera_default"))
                arraylist.remove(i);
        }

    }*/

    private void goImg() {
        Intent intent = new Intent(this, MultiImageSelectorActivity.class);
        // 是否显示调用相机拍照
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);

// 最大图片选择数量
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);

// 设置模式 (支持 单选/MultiImageSelectorActivity.MODE_SINGLE 或者 多选/MultiImageSelectorActivity.MODE_MULTI)
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);

        startActivityForResult(intent, REQUEST_IMAGE);
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://test.sgone.cn.mulimagedemo/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://test.sgone.cn.mulimagedemo/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
